﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class BetPage : EventManager
{
    private static BetPage instance;
    public static BetPage GetInstance()
    {
        return instance;
    }
    void Awake()
    {
        instance = this;
    }

    public override void InformationSetting()
    {
        AddListenerToButton(addScore1Btn, addScoreBtn1Click);
        AddListenerToButton(addScore2Btn, addScoreBtn2Click);
        AddListenerToButton(addScore3Btn, addScoreBtn3Click);
        AddListenerToButton(oneTimeBtn, OneTimeBtnClick);
        SetText(addScore1Tex, (GameRoomModel.GetInstance().BotScore).ToString());
        SetText(addScore2Tex, (GameRoomModel.GetInstance().BotScore*2).ToString());
        SetText(addScore3Tex, (GameRoomModel.GetInstance().BotScore*5).ToString());

        CheckAddBtn();

        if (GameRoomModel.GetInstance().OneTimesBet != 0)
        {
            oneTimeBtn.gameObject.SetActive(true);
        }
    }

    public Button addScore1Btn;
    public Button addScore2Btn;
    public Button addScore3Btn;
    public Text addScore1Tex;
    public Text addScore2Tex;
    public Text addScore3Tex;
    public Text appendTex;
    public Button oneTimeBtn;
    public Slider oneTimeBetSlider;
    private int betNum;
    public GameObject topStop;

    private void CheckAddBtn()
    {
        if (GameRoomModel.GetInstance().playerDic[GameData.GetInstance().locationIndex].TotalScore >= GameRoomModel.GetInstance().BotScore * 25)
        {
            addScore1Btn.gameObject.GetComponent<Button>().interactable = true;
            addScore2Btn.gameObject.GetComponent<Button>().interactable = true;
            addScore3Btn.gameObject.GetComponent<Button>().interactable = true;
        }
        else if (GameRoomModel.GetInstance().playerDic[GameData.GetInstance().locationIndex].TotalScore < GameRoomModel.GetInstance().BotScore * 25 && GameRoomModel.GetInstance().playerDic[GameData.GetInstance().locationIndex].TotalScore >= GameRoomModel.GetInstance().BotScore*10)
        {
            addScore1Btn.gameObject.GetComponent<Button>().interactable = true;
            addScore2Btn.gameObject.GetComponent<Button>().interactable = true;
            addScore3Btn.gameObject.GetComponent<Button>().interactable = false;
        }
        else if (GameRoomModel.GetInstance().playerDic[GameData.GetInstance().locationIndex].TotalScore < GameRoomModel.GetInstance().BotScore * 10 && GameRoomModel.GetInstance().playerDic[GameData.GetInstance().locationIndex].TotalScore >= GameRoomModel.GetInstance().BotScore*5)
        {
            addScore1Btn.gameObject.GetComponent<Button>().interactable = true;
            addScore2Btn.gameObject.GetComponent<Button>().interactable = false;
            addScore3Btn.gameObject.GetComponent<Button>().interactable = false;
        }
        else if (GameRoomModel.GetInstance().playerDic[GameData.GetInstance().locationIndex].TotalScore < GameRoomModel.GetInstance().BotScore * 5 && GameRoomModel.GetInstance().playerDic[GameData.GetInstance().locationIndex].TotalScore >= GameRoomModel.GetInstance().BotScore)
        {
            addScore1Btn.gameObject.GetComponent<Button>().interactable = false;
            addScore2Btn.gameObject.GetComponent<Button>().interactable = false;
            addScore3Btn.gameObject.GetComponent<Button>().interactable = false;
        }
        else if (GameRoomModel.GetInstance().playerDic[GameData.GetInstance().locationIndex].TotalScore < GameRoomModel.GetInstance().BotScore)
        {
            addScore1Btn.gameObject.GetComponent<Button>().interactable = false;
            addScore2Btn.gameObject.GetComponent<Button>().interactable = false;
            addScore3Btn.gameObject.GetComponent<Button>().interactable = false;
            MessageManager.GetInstance().SendMsg1212(2, GameData.GetInstance().locationIndex, GameRoomModel.GetInstance().playerDic[GameData.GetInstance().locationIndex].TotalScore);
        }
    }

    private void addScoreBtn1Click()
    {
        MessageManager.GetInstance().SendMsg1212(1, GameData.GetInstance().locationIndex, GameRoomModel.GetInstance().BotScore);

        CloseBtnClick();

        GameData.GetInstance().SetIsClickThisChapterTrue();
    }
    private void addScoreBtn2Click()
    {
        MessageManager.GetInstance().SendMsg1212(1, GameData.GetInstance().locationIndex, GameRoomModel.GetInstance().BotScore *2);

        CloseBtnClick();

        GameData.GetInstance().SetIsClickThisChapterTrue();
    }
    private void addScoreBtn3Click()
    {
        MessageManager.GetInstance().SendMsg1212(1, GameData.GetInstance().locationIndex, GameRoomModel.GetInstance().BotScore * 5);

        CloseBtnClick();

        GameData.GetInstance().SetIsClickThisChapterTrue();
    }

    private void OneTimeBtnClick()
    {
        oneTimeBtn.gameObject.SetActive(false);
        oneTimeBetSlider.gameObject.SetActive(true);

        GameData.GetInstance().SetIsClickThisChapterTrue();
    }

    public void AppendBtnDownEvent()
    {
        ShowAppendText(0);
        //GameRoomPage.GetInstance().SetAudAndPlay(6);
    }

    public void AppendBtnUpEvent()
    {
        int temp = (int)Math.Floor(oneTimeBetSlider.value * GameRoomModel.GetInstance().OneTimesBet*GameRoomModel.GetInstance().BotScore);

        if (temp > GameRoomModel.GetInstance().playerDic[GameData.GetInstance().locationIndex].TotalScore)
        {
            temp = GameRoomModel.GetInstance().playerDic[GameData.GetInstance().locationIndex].TotalScore;
        }
        else
        {
            temp = ((int)(temp / GameRoomModel.GetInstance().BotScore)) * GameRoomModel.GetInstance().BotScore;
        }

        if (temp > 0)
        {
            MessageManager.GetInstance().SendMsg1212(2, GameData.GetInstance().locationIndex, temp);
            CloseBtnClick();
        }
        else
        {
            oneTimeBetSlider.gameObject.SetActive(false);
            oneTimeBtn.gameObject.SetActive(true);
        }
    }

    public void OnSliderValueChange()
    {
        int temp = (int)Math.Floor(oneTimeBetSlider.value * GameRoomModel.GetInstance().OneTimesBet *GameRoomModel.GetInstance().BotScore);

        if (temp > GameRoomModel.GetInstance().playerDic[GameData.GetInstance().locationIndex].TotalScore)
        {
            temp = GameRoomModel.GetInstance().playerDic[GameData.GetInstance().locationIndex].TotalScore;
        }
        else
        {
            temp = ((int)(temp / GameRoomModel.GetInstance().BotScore)) * GameRoomModel.GetInstance().BotScore;
        }

        if (oneTimeBetSlider.value == 1)
        {
            topStop.SetActive(true);
        }
        else
        {
            topStop.SetActive(false);
        }
        SetText(appendTex, temp.ToString());


        GameData.GetInstance().SetIsClickThisChapterTrue();
    }

    private void ShowAppendText(int num)
    {
        appendTex.text = num.ToString();
    }

    private bool isPlayAud;
    private float timer = 0;
    void Update()
    {
        if (GameRoomModel.GetInstance().OneTimesBet == 0 )
        {
            if (oneTimeBetSlider.value < 1)
            {
                topStop.SetActive(false);
                int temp = (int)Math.Floor(oneTimeBetSlider.value * GameRoomModel.GetInstance().OneTimesBet);
                ShowAppendText(temp);
            }
            else
            {
                topStop.SetActive(true);
            }
        }

        if (isPlayAud)
        {
            timer += Time.deltaTime;
            if (timer > 1)
            {
                isPlayAud = false;
                timer = 0;
            }
        }
    }
}
